import axios from '@/libs/api.request'

export default {
  methods: {
    handleFormatError (file) {
      this.$Notice.warning({
        title: 'The file format is incorrect',
        desc: 'File format of ' + file.name + ' is incorrect, please select jpg or png.'
      })
    },
    handleMaxSize (file) {
      this.$Notice.warning({
        title: 'Exceeding file size limit',
        desc: 'File  ' + file.name + ' is too large, no more than 2M.'
      })
    },
    handleBeforeUpload () {
      const check = this.uploadList.length < 5
      if (!check) {
        this.$Notice.warning({
          title: 'Up to five pictures can be uploaded.'
        })
      }
      return check
    },
    apiUrl (url) {
      return process.env.hostAddress + url
    },
    absUrl (url) {
      return this.imgUrl(url)
    },
    imgUrl (url) {
      if (url.indexOf('://') !== -1) {
        return url
      }
      return (process.env.NODE_ENV === 'dev' ? 'http://127.0.0.1:8881' : '') + url
    },

    getCommonHeaders () {
      return {

      }
    },
    showLoading () {
      this.loadingObj = this.$Message.loading({
        content: '加载中...',
        duration: 0
      })
    },
    hideLoading () {
      if (this.loadingObj) {
        this.loadingObj()
        this.loadingObj = null
      }
    },
    post (url, data) {
      return axios.request({
        url,
        method: 'post',
        data
      })
    },
    datetimeStr (date) {
      if (!date) {
        return ''
      }
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let d = date.getDate()
      let h = date.getHours()
      let m = date.getMinutes()
      let s = date.getSeconds()
      return year + '-' + this.wd(month) + '-' + this.wd(d) + ' ' + this.wd(h) + ':' + this.wd(m) + ':' + this.wd(s)
    },
    dateStr (date) {
      if (!date) {
        return ''
      }
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let d = date.getDate()
      return year + '-' + this.wd(month) + '-' + this.wd(d)
    },
    wd (w) {
      return w > 9 ? w : '0' + w
    },
    getToken () {
      return util.getToken()
    },
    get (url, params) {
      return axios.request({
        url,
        method: 'get',
        params
      })
    }
  }
}
